package com.feng.oauth2.oauth.infrastructure.jdbc;


import com.feng.oauth2.oauth.domain.resource.ResourceIds;
import com.feng.oauth2.oauth.domain.resource.ResourceIdsRepository;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository("resourceIdsRepositoryJdbc")
public class ResourceIdsRepositoryJdbc implements ResourceIdsRepository {


    private static ResourceIdsRowMapper rowMapper = new ResourceIdsRowMapper();

    @Autowired
    private JdbcTemplate jdbcTemplate;


    @Override
    public ResourceIds findResourceIdsById(Integer id) {
        final String sql = " select * from resource_ids where  id = ? ";
        final List<ResourceIds> list = this.jdbcTemplate.query(sql, new Object[]{id}, rowMapper);

        ResourceIds resourceIds = null;
        if (!list.isEmpty()) {
            resourceIds = list.get(0);
        }

        return resourceIds;
    }

    @Override
    public List<ResourceIds> findAllResourceIds() {
        final String sql = " select * from resource_ids ";
        return this.jdbcTemplate.query(sql, rowMapper);
    }

    @Override
    public void saveResourceIds(ResourceIds resourceIds) {
        final String sql = " insert into resource_ids(resource-id,resource-name)  values (?,?) ";
        this.jdbcTemplate.update(sql, resourceIds.getResourceId(), resourceIds.getResourceName());
        
    }

    @Override
    public void updateResourceIds(ResourceIds resourceIds) {
        final String sql = "update resource_ids set resource-id=?,resource-name=? where id=? ";
        this.jdbcTemplate.update(sql, resourceIds.getResourceId(), resourceIds.getResourceName(),resourceIds.getId());
    }

    @Override
    public void deleteResourceIds(Integer id) {
        final String sql = "delete from resource_ids where id=? ";
        this.jdbcTemplate.update(sql, id);
    }
}
